function rotate_matrix(xz_sin, xz_cos, y_sin, y_cos){
	let rotate = [
		xz_cos, y_sin * xz_sin, y_cos * xz_sin, 0.0,
		0.0, y_cos, -y_sin, 0.0,
		-xz_sin, y_sin * xz_cos, y_cos * xz_cos, 0.0,
		0.0, 0.0, 0.0, 1.0];
	
	/*let rotate = [
		xz_cos * y_cos, xz_cos * y_sin, xz_sin, 0.0,
		-y_sin, y_cos, 0.0, 0.0,
		-xz_sin * y_cos, -xz_sin * y_sin, y_cos, 0.0,
		0.0, 0.0, 0.0, 1.0];*/
	
	return rotate;
}

function rotate_matrix_xz(xz_sin, xz_cos){
	let rotate = [
		xz_cos, 0, xz_sin, 0.0,
		0.0, 1.0, 0.0, 0.0,
		-xz_sin, 0.0, xz_cos, 0.0,
		0.0, 0.0, 0.0, 1.0];
	
	/*let rotate = [
		xz_cos * y_cos, xz_cos * y_sin, xz_sin, 0.0,
		-y_sin, y_cos, 0.0, 0.0,
		-xz_sin * y_cos, -xz_sin * y_sin, y_cos, 0.0,
		0.0, 0.0, 0.0, 1.0];*/
	
	return rotate;
}

//由向量得到目标旋转矩阵
function rotate_matrix_vec(x, y, z){
	let xz = Math.sqrt(Math.pow(x, 2) + Math.pow(z, 2));
	let xyz = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
	
	let xz_sin = z / Math.max(xz, 0.00001);//使用max 防止除以0的情况
	let xz_cos = x / Math.max(xz, 0.00001);
	let y_sin = y / Math.max(xyz, 0.00001);
	let y_cos = xz / Math.max(xyz, 0.00001);
	
	let rotate = [
		xz_cos * y_cos, y_sin, y_cos * xz_sin, 0.0,
		-xz_cos * y_sin, y_cos, -y_sin * xz_sin, 0.0,
		-xz_sin, 0.0, xz_cos, 0.0,
		0.0, 0.0, 0.0, 1.0];
	
	return rotate;
}

//四元数转换到旋转矩阵
function quad2matrix(b, c, d, a){
	let rotate = [
		1-2*c*c-2*d*d,		2*b*c+2*a*d,		2*b*d-2*a*c,	0.0,
		2*b*c-2*a*d,		1-2*b*b-2*d*d,	2*a*b+2*c*d,	0.0,
		2*a*c+2*b*d,		2*c*d-2*a*b,		1-2*b*b-2*c*c,0.0,
		0.0,				0.0,				0.0,			1.0
	];
	
	return rotate;
}

//距离计算函数
function vec_norm2(x, y, z){
	let xyz = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
	return xyz;
}
function vec_norm22(x, y, z){
	let xyz = Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
	return xyz;
}
//动能计算
function kinetic_cal(x, y, z){
	return (vec_norm22(x, y, z) / 2.0);
}

//叉乘
function normal_gen( v1,  v2){
	let result_norm = [0,0,0];
	
	result_norm[0] = v1[1]*v2[2]-v1[2]*v2[1];
	result_norm[1]= v1[2]*v2[0]-v1[0]*v2[2];
	result_norm[2] = v1[0]*v2[1]-v1[1]*v2[0];
	
	//result_norm = normalize(result_norm);
	//归一化
	const length = Math.sqrt(result_norm[0] * result_norm[0] + result_norm[1] * result_norm[1] + result_norm[2] * result_norm[2]);
	result_norm[0] /= length;
	result_norm[1] /= length;
	result_norm[2] /= length;
	
	return result_norm;
}

//矩阵乘法
function mul_matrix(a, b){
	//a b列主序4*4矩阵 结果是矩阵乘法a*b
	let result = [];
	
	//循环中 col row 表示矩阵列与行索引 区分列主序的矩阵表示方式
	for(let col = 0;col < 4;col += 1){
		for(let row = 0;row < 4;row += 1){
			result[col * 4 + row] = a[row] * b[col * 4 + 0] 
							+ a[4 + row] * b[col * 4 + 1]
							+ a[4 * 2 + row] * b[col * 4 + 2]
							+ a[4 * 3 + row] * b[col * 4 + 3];
		}
	}
	
	return result;
}

function mul_matrix_vec(a, b){
	//输出4向量
	let result = [];
	
	for(let row = 0;row < 4;row += 1){
		result[row] = a[row] * b[0] 
					+ a[4 + row] * b[1]
					+ a[4 * 2 + row] * b[2]
					+ a[4 * 3 + row] * b[3];
	}
	
	return result;
}

//合并一系列变换矩阵
function fire_trans_matrix_gen(obj_move, partial_move, obj_rota, partial_rota, cam_matrix){
	let move = [1, 0, 0, 0,
			0, 1, 0, 0,
			0, 0, 1, 0,
			0, 0, 0, 1];
	
	//火焰相对火箭的位移
	move[12] = obj_move[0];
	move[13] = obj_move[1];
	move[14] = obj_move[2];
	let result = mul_matrix(move, obj_rota);
	result = mul_matrix(partial_rota, result);
	
	//火箭的位移
	move[12] = partial_move[0];
	move[13] = partial_move[1];
	move[14] = partial_move[2];
	result = mul_matrix(move, result);
	
	result = mul_matrix(cam_matrix, result);
	
	return result;
	
}

function rocket_trans_matrix_gen(partial_move, partial_rota, cam_matrix){
	let move = [1, 0, 0, 0,
			0, 1, 0, 0,
			0, 0, 1, 0,
			0, 0, 0, 1];
	
	move[12] = partial_move[0];
	move[13] = partial_move[1];
	move[14] = partial_move[2];
	
	let result = mul_matrix(move, partial_rota);
	result = mul_matrix(cam_matrix, result);
	
	return result;
}

//透视 镜头旋转 移动矩阵 合并
function cam_matrix_gen(global_move, sight_rota, pers_matrix){
	let move = [1, 0, 0, 0,
			0, 1, 0, 0,
			0, 0, 1, 0,
			0, 0, 0, 1];
	
	move[12] = global_move[0];
	move[13] = global_move[1];
	move[14] = global_move[2];
	
	//let result = mul_matrix(move, result);
	let result = mul_matrix(sight_rota, move);
	result = mul_matrix(pers_matrix, result);
	
	return result;
}